package edu.purdue.cs505;

import java.util.HashSet;
import java.util.Set;

public class Message
{
  int messageNumber;
  String contents;
  String processID;
  Set<Integer> obsoleted = new HashSet<Integer>();

  public final int getMessageNumber()
  {
    return messageNumber;
  }

  public final void setMessageNumber(int messageNumber)
  {
    this.messageNumber = messageNumber;
  }

  public final String getContents()
  {
    return contents;
  }

  public final void setContents(String contents)
  {
    this.contents = contents;
  }

  public final String getProcessID()
  {
    return processID;
  }

  public final void setProcessID(String processID)
  {
    this.processID = processID;
  }
  
  public String toString()
  {
    return Integer.toString(messageNumber) + "@" + processID + "@" + contents + "@" + obsoleted.toArray();
  }

  // For FIFO-SRB
  public void makesObsolete(int messageNumber)
  {
    obsoleted.add(messageNumber);
  }

  // For FIFO-SRB
  public int[] getObsoletedMessages()
  {
    int[] result = new int[obsoleted.size()];
    int i = 0;
    for (Integer integer : obsoleted) {
      result[i++] = integer;
    }
    return result;
  }

  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((contents == null) ? 0 : contents.hashCode());
    result = prime * result + messageNumber;
    result = prime * result + ((processID == null) ? 0 : processID.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj)
  {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    Message other = (Message) obj;
    if (contents == null) {
      if (other.contents != null) return false;
    }
    else if (!contents.equals(other.contents)) return false;
    if (messageNumber != other.messageNumber) return false;
    if (processID == null) {
      if (other.processID != null) return false;
    }
    else if (!processID.equals(other.processID)) return false;
    return true;
  }

}
